//
//  PersonCell.swift
//  App_Vision
//
//  Created by Flutter on 2021/8/6.
//

import UIKit
import UICore
import Leaf
import Center

class PersonCell: CollectionViewCell {
    
    enum Style {
        case Empty
        case Content
        case Add
    }
    
    var style: Style = .Content {
        didSet {
            if style == .Empty {
                contentView.isHidden = true
            }else if style == .Add {
                contentView.isHidden = false
                addImageView.isHidden = false
                avatarImageView.isHidden = true
                nameLabel.isHidden = true
            }else {
                contentView.isHidden = false
                addImageView.isHidden = true
                avatarImageView.isHidden = false
                nameLabel.isHidden = false
            }
        }
    }
    
    var person: Person? {
        didSet {
            nameLabel.text = person?.name
            
            if let path = person?.avatar {
                avatarImageView.image = UIImage(contentsOfFile: path)
            }else {
                avatarImageView.image = UIImage.image(named: "avatar_default")
            }
        }
    }
    
    lazy var avatarImageView: UIImageView = {
        UIImageView(image: UIImage.image(named: "avatar_default"))
            .leaf
            .shadow(color: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1), offset: CGSize(width: 5, height: 5), opacity: 0.8, radius: 10)
            .instance
    }()
    lazy var addImageView: UIImageView = {
        UIImageView(image: UIImage.image(named: "person_add"))
            .leaf
            .shadow(color: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1), offset: CGSize(width: 5, height: 5), opacity: 0.8, radius: 10)
            .instance
    }()
    
    lazy var nameLabel: UILabel = {
        UILabel()
            .leaf
            .text("默认")
            .font(.systemFont(ofSize: 20))
            .textColor(.black)
            .instance
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}



private extension PersonCell {
   
    func setupUI() {
        
//        contentView.backgroundColor = .systemBlue
        avatarImageView.layer.cornerRadius = contentView.bounds.width/2
        avatarImageView.leaf.add(to: contentView).snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.centerY.equalToSuperview().offset(-20)
            $0.width.equalToSuperview()
            $0.height.equalTo(avatarImageView.snp.width)
        }
        
        
        
        nameLabel.leaf.add(to: contentView).snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.top.equalTo(avatarImageView.snp.bottom).offset(10)
            
        }
        
        addImageView.layer.cornerRadius = contentView.bounds.width/2
        addImageView.leaf.add(to: contentView).snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.centerY.equalToSuperview().offset(-10)
            $0.width.equalToSuperview()
            $0.height.equalTo(avatarImageView.snp.width)
        }
        
    }
    
}
